$(function() {
    var npwd = $( "#first" ),
    vpwd = $( "#second" ),
    allFields = $( [] ).add( npwd ).add( vpwd ),
    tips = $( ".validateTips" );
 
    function updateTips( t ) {
        tips
        .text( t )
        .addClass( "ui-state-highlight" );
        setTimeout(function() {
            tips.removeClass( "ui-state-highlight", 1500 );
        }, 500 );
    }
    // Check if password are equal
    function checkPassword(first, second){
        if (first.val() != second.val()){
            second.addClass( "ui-state-error" );
            updateTips( "Les mot de passes doivent être identiques");
            return false;
        }else {
            return true;
        }
    }
 
    $( "#dialog-form-profil" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        resizable: false,
        modal: true,
        buttons: {
            "Changer": function() {
                var bValid = true;
                allFields.removeClass( "ui-state-error" );
                
                bValid = bValid && checkPassword(npwd, vpwd);

                if ( bValid ) {
                    var url = $( "#redirectpwd" ).val();
                    $.post(url, allFields, function(o){
                        //alert(1);
                        });
                    $( this ).dialog( "close" );
                }
            },
            Annuler: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
 
    $( "#edit-profil" )
    .button()
    .click(function() {
        $( "#dialog-form-profil" ).dialog( "open" );
    });
});

// Show information message Web site
$(function() {
    $( "#dialog-message" ).dialog({
        autoOpen: false,
        height: 600,
        width: 700,
        modal: false,
        buttons: {
            Ok: function() {
                $( this ).dialog( "close" );
            }
        }
    });
    

    $( "#open-message" )
    .button()
    .click(function() {
        $( "#dialog-message" ).dialog( "open" );
    });
});
    
// Login Database popup
$(function() {
    var selectDB, login, password, allFields;
    
    // Check if password are equal
    function checkField(o){
        if ( o.val().length > 0 ) {
            o.removeClass( "ui-state-error" );
            return true;
        } else {
            o.addClass( "ui-state-error" );
            updateTips( "Tous les champs sont obligatoires");
            return false;
        }
    }
    
    function updateTips( t ) {
        tips
        .text( t )
        .addClass( "ui-state-highlight" );
        setTimeout(function() {
            tips.removeClass( "ui-state-highlight", 1500 );
        }, 500 );
    }
    
    $( ".bddLink" ).button().click(function() {
        $("#dbselect").val($(this).val());
        login = $( "#userDB" );
        password = $( "#pwdDB" );
        selectDB = $("#dbselect");
        allFields = $( [] ).add( login ).add( password ).add(selectDB),
        tips = $( ".validateTips" );
        $( "#dialog-db" ).dialog( "open" );
    });
    
    $( "#dialog-db" ).dialog({
        autoOpen: false,
        height: 400,
        width: 300,
        resizable: false,
        modal: false,
        buttons: {
            Ok: function() {
                var bValid = true;
                bValid = bValid && checkField(login);
                bValid = bValid && checkField(password);
                
                if ( bValid ) {
                    var url = $( "#changeDB" ).val();
                    $.post(url, allFields, function(o){
                        window.location.reload();
                        });
                    $( this ).dialog( "close" );
                }
            },
            Annuler: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
});